////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Process;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cc.telecomdigital.tdstock.ExceptionHandler;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.Activity_Framework;

/**
 * login screen entry
 * @author haibo
 *
 */
public class RegisterActivity extends Activity_Framework implements OnClickListener
{
	protected static final String	ACTION_INSTALL_SHORTCUT	= "com.android.launcher.action.INSTALL_SHORTCUT";
	protected static final String	EXTRA_SHORTCUT_DUPLICATE	= "duplicate";

	private Button				_webEntry;
	private Button				_salesDept;
	private Button				_phoneEntry;
	//	private Button  _logonEntry;
	private Button				_returnPre;

	private static String		TAG					= RegisterActivity.class
														.getSimpleName();

	protected static String		_menu_exit_app_res		= null;
	protected static String		_menu_gen_launcher_res	= null;

	public static final int		EXIT_APP				= Menu.FIRST;
	public static final int		CREATE_LAUNCHER		= Menu.FIRST + 1;

	@Override
	public void onClick(View v)
	{

		try {
			if (v == _webEntry)
			{
				Uri uri = Uri
						.parse("http://www.telecomdigital.cc/enroll/ienroll.do?device_type=1&view=gphone");
				Intent gotoPage = new Intent(Intent.ACTION_VIEW, uri);
				gotoPage.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				startActivity(gotoPage);
			}
			else if (v == _salesDept)
			{
				Uri uri = Uri
						.parse("http://www.telecomdigital.cc/enroll/iphone_address.htm");
				Intent gotoPage = new Intent(Intent.ACTION_VIEW, uri);
				gotoPage.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				startActivity(gotoPage);
			}
			else if (v == _phoneEntry)
			{
				Uri uri = Uri.parse("tel:77787778");
				Intent callHotLine = new Intent(Intent.ACTION_DIAL, uri);
				callHotLine.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				startActivity(callHotLine);
			}
			/*else if(v==_logonEntry) {
				Intent intent = new Intent(getApplicationContext(), AccountLoginActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent); 
				
				
			}*/else if (v == _returnPre)
			{

				Intent intent = new Intent(getApplicationContext(),
						RealtimeStockActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				//startActivity(intent);
				SwitchFreeActivity(RealtimeStockActivity.class , intent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ExceptionHandler.register(this);
		this.setContentView(R.layout.register);
		_webEntry = (Button) findViewById(R.id.td_stock_register_web_entry);
		_salesDept = (Button) findViewById(R.id.retail_sales_department);
		_phoneEntry = (Button) findViewById(R.id.td_stock_register_phone_entry);
		//		_logonEntry = (Button)findViewById(R.id.td_stock_logon_entry);
		_returnPre = (Button) findViewById(R.id.btn_returnpre);

		_webEntry.setOnClickListener(this);
		_salesDept.setOnClickListener(this);
		_phoneEntry.setOnClickListener(this);
		//		_logonEntry.setOnClickListener(this);
		_returnPre.setOnClickListener(this);

		_menu_exit_app_res = getResources().getString(R.string.app_exit);
		_menu_gen_launcher_res = getResources().getString(
				R.string.app_launcher);
	}

	protected synchronized void handleExitApplication()
	{
		new AlertDialog.Builder(this).setTitle(
				getResources().getString(R.string.info)).setMessage(
				getResources().getString(R.string.application_exit_tips))
				.setIcon(
						getResources().getDrawable(
								R.drawable.help_menu_icon))
				.setPositiveButton(getResources().getString(R.string.ok),
						new DialogInterface.OnClickListener()
						{
							public void onClick(
									DialogInterface dialoginterface,
									int i)
							{
								((ITDLApplication) getApplicationContext())
										.setExitApp(true);
								exitApp();
							}
						}).setNegativeButton(
						getResources().getString(R.string.cancel),
						new DialogInterface.OnClickListener()
						{
							public void onClick(DialogInterface dialog,
									int whichButton)
							{

							}
						}).show();

	}

	public void exitApp()
	{
		finish();
		Process.sendSignal(Process.myPid(), Process.SIGNAL_QUIT);
	}

	protected synchronized void genLauncher()
	{
		Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getString(R.string.app_name));
		shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
		Intent intent2 = new Intent(Intent.ACTION_MAIN);
		intent2.addCategory(Intent.CATEGORY_LAUNCHER);

		// intent2.setComponent(new ComponentName(this.getPackageName(),    ".activity.LoginActivity")); 
		intent2.setComponent(new ComponentName(this.getPackageName(),
				".activity.SplashActivity"));

		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
				Intent.ShortcutIconResource.fromContext(this,
						R.drawable.icon));
		sendBroadcast(shortcutIntent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		menu.add(0, CREATE_LAUNCHER, 0, _menu_gen_launcher_res);
		MenuItem mItem = menu.findItem(CREATE_LAUNCHER);
		mItem.setIcon(R.drawable.back_down);
		menu.add(0, EXIT_APP, 1, _menu_exit_app_res);
		mItem = menu.findItem(EXIT_APP);
		mItem.setIcon(R.drawable.down_24_1);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case EXIT_APP:
				((ITDLApplication) getApplicationContext())
						.setExitApp(true);
				exitApp();
				break;
			case CREATE_LAUNCHER:
				genLauncher();
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume()
	{
		// TODO Auto-generated method stub
		if (((ITDLApplication) getApplicationContext()).isExitApp())
		{
			exitApp();
		}
		/*else {
			Class<?> currentActivityClass = ((ITDLApplication)getApplicationContext()).getCurrentActivityClass();
			if(currentActivityClass != null) {
				Intent intent = new Intent(getApplicationContext(), currentActivityClass);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);   //continuing to get the last activity
			}
		}*/

		super.onResume();
	}

	@Override
	protected void onRestart()
	{
		// TODO Auto-generated method stub
		super.onRestart();

		if (((ITDLApplication) getApplicationContext()).isExitApp())
		{
			exitApp();
		}
	}

}
